/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package com.civet.myadmin.modules.goods.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.civet.myadmin.common.config.Global;
import com.civet.myadmin.common.persistence.Page;
import com.civet.myadmin.common.web.BaseController;
import com.civet.myadmin.common.utils.StringUtils;
import com.civet.myadmin.modules.goods.entity.GoodsCategory;
import com.civet.myadmin.modules.goods.service.GoodsCategoryService;

import java.util.List;
import java.util.Map;

/**
 * 商品分类Controller
 * @author Jockey
 * @version 2016-11-20
 */
@Controller
@RequestMapping(value = "${adminPath}/goods/goodsCategory")
public class GoodsCategoryController extends BaseController {

	@Autowired
	private GoodsCategoryService goodsCategoryService;


    @ModelAttribute
    public GoodsCategory get(@RequestParam(required=false) Integer id) {
        if (id != null){
            return goodsCategoryService.get(id);
        }else{
            return new GoodsCategory();
        }
    }

	@RequiresPermissions("goods:goodsCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(GoodsCategory goodsCategory,  Model model) {
		model.addAttribute("list", goodsCategoryService.findAllGoodsCategory());
		return "modules/goods/goodsCategoryList";
	}

//	@RequiresPermissions("goods:goodsCategory:view")
//	@RequestMapping(value = {"list", ""})
//	public String list(GoodsCategory goodsCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<GoodsCategory> page = goodsCategoryService.findPage(new Page<GoodsCategory>(request, response), goodsCategory);
//		model.addAttribute("page", page);
//		return "modules/goods/goodsCategoryList";
//	}

	@RequiresPermissions("goods:goodsCategory:view")
	@RequestMapping(value = "form")
	public String form(GoodsCategory goodsCategory, Model model) {
		if(goodsCategory.getParent()==null || goodsCategory.getParent().getId()==null){
			goodsCategory = new GoodsCategory();
		}else{
			goodsCategory.setParent(goodsCategoryService.get(goodsCategory.getParent().getId()) );
			if(goodsCategory.getId()==null){
				int size = 0;
				List<GoodsCategory> list = goodsCategoryService.findAllGoodsCategory();
				for (int i=0; i<list.size(); i++){
					GoodsCategory gc = list.get(i);
					if (gc.getParent()!=null && gc.getParent().getId()!=null
							&& gc.getParent().getId().equals(goodsCategory.getParent().getId())){
						size++;
					}
				}
			}
		}

//	    if (goodsCategory.getId() != null){
//            goodsCategory = goodsCategoryService.get(goodsCategory.getId());
//        }
		model.addAttribute("goodsCategory", goodsCategory);
		return "modules/goods/goodsCategoryForm";
	}

	@RequiresPermissions("goods:goodsCategory:edit")
	@RequestMapping(value = "save")
	public String save(GoodsCategory goodsCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, goodsCategory)){
			return form(goodsCategory, model);
		}
		goodsCategoryService.save(goodsCategory);
		addMessage(redirectAttributes, "保存商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/goods/goodsCategory/?repage";
	}

	@RequiresPermissions("goods:goodsCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(GoodsCategory goodsCategory, RedirectAttributes redirectAttributes) {
		goodsCategoryService.delete(goodsCategory);
		addMessage(redirectAttributes, "删除商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/goods/goodsCategory/?repage";
	}




    @RequiresPermissions("goods:goodsCategory:edit")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String module, @RequestParam(required=false) String extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<GoodsCategory> list = goodsCategoryService.findAllGoodsCategory();
		for(int i=0;i<list.size();i++){
			GoodsCategory gc = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(gc.getId()) && gc.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", gc.getId());
				map.put("pId", gc.getParentId());
				map.put("name", gc.getCategoryName());
				map.put("categoryCode",gc.getCategoryCode()!=null?gc.getCategoryCode():"");
				map.put("remarks",gc.getRemarks()!=null?gc.getRemarks():"");
				mapList.add(map);
			}
		}

		return mapList;
	}

}